package com.yc.UI;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.wb.swt.SWTResourceManager;

import com.yc.utils.Values;
import com.yc.dao.UserDao;
import com.yc.utils.SWTUtil;

import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.widgets.Button;


/**
 *主界面
 * @author UNO
 * 2021年6月22日下午7:25:21
 */
public class MainUI {

	protected Shell shell;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			MainUI window = new MainUI();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		shell.setImage(SWTResourceManager.getImage(WelcomeUI.class, "/images/福字.png"));	
		shell.setSize(1063, 681);
		shell.setText("12306");
		// 设置窗口居中
		SWTUtil.centerWindom(shell);
		shell.setLayout(new FillLayout(SWT.HORIZONTAL));

		SashForm sashForm = new SashForm(shell, SWT.NONE);	
		sashForm.setOrientation(SWT.VERTICAL);

		Composite composite = new Composite(sashForm, SWT.BORDER);

		composite.setBackgroundMode(SWT.INHERIT_FORCE);
		composite.setBackgroundImage(SWTResourceManager.getImage(MainUI.class, "/images/titleback.png"));


		Label lblNewLabel = new Label(composite, SWT.NONE);
		lblNewLabel.setBackgroundImage(SWTResourceManager.getImage(MainUI.class, "/images/title.png"));
		lblNewLabel.setBounds(10, 10, 130, 80);

		Label lblNewLabel_2 = new Label(composite, SWT.CENTER);
		lblNewLabel_2.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 15, SWT.NORMAL));
		lblNewLabel_2.setBounds(347, 36, 370, 33);
		lblNewLabel_2.setText("尊敬的 " + Values.currentLoginUser.get("u_name") + " 欢迎使用12306");


		//lblNewLabel_2.setLocation(20, 30);
		//标签居中未完成
		//		SWTUtil.centerLabel(composite, lblNewLabel_2);

		Composite composite_1 = new Composite(sashForm, SWT.NONE);
		composite_1.setBackgroundMode(SWT.INHERIT_DEFAULT);

		composite_1.setBackgroundImage(SWTResourceManager.getImage(MainUI.class, "/images/背景.jpg"));
		composite_1.setLayout(new FillLayout(SWT.HORIZONTAL));
		//composite_1默认 宽度1063  高度为531 
		//图片自适应
		SWTUtil.imagesSize(composite_1);

		SashForm sashForm_1 = new SashForm(composite_1, SWT.NONE);
		sashForm_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_TRANSPARENT));

		Composite composite_2 = new Composite(sashForm_1, SWT.BORDER);
		composite_2.setBackground(SWTResourceManager.getColor(SWT.COLOR_TRANSPARENT));

		Button button1 = new Button(composite_2, SWT.CENTER);		
		button1.setBounds(40, 80, 98, 30);
		button1.setText("个人中心");

		Button button3 = new Button(composite_2, SWT.CENTER);
		button3.setBounds(40, 283, 98, 30);
		button3.setText("购票");

		Button button4 = new Button(composite_2, SWT.CENTER);
		button4.setBounds(40, 383, 98, 30);
		button4.setText("查询订单");

		Button button2 = new Button(composite_2, SWT.CENTER);
		button2.setBounds(40, 183, 98, 30);
		button2.setText("联系人");

		Composite composite_3 = new Composite(sashForm_1, SWT.BORDER);
		composite_3.setBackground(SWTResourceManager.getColor(SWT.COLOR_TRANSPARENT));
		composite_3.setBackgroundMode(SWT.INHERIT_FORCE);
		composite_3.setLayout(Values.stackLayout);

		Composite composite_4 = new Composite(sashForm, SWT.NONE);

		Button button_3 = new Button(composite_4, SWT.CENTER);
		button_3.setBounds(742, 3, 98, 30);
		button_3.setText("退出登陆");

		Button button_4 = new Button(composite_4, SWT.NONE);
		button_4.setBounds(918, 3, 98, 30);
		button_4.setText("注销账号");

		Label label = new Label(composite_4, SWT.NONE);
		label.setBounds(10, 10, 186, 20);
		label.setText("联系客服：15789895473");

		Label label_1 = new Label(composite_4, SWT.NONE);
		label_1.setBounds(243, 10, 219, 20);

		label_1.setText("地址：衡阳市红湘街道南华大学");

		sashForm.setWeights(new int[] {102, 490, 36});
		//默认副界面是个人中心
		UserInfoUI userInfoUI = new UserInfoUI(composite_3,SWT.NONE);
		userInfoUI.setBackground(SWTResourceManager.getColor(SWT.COLOR_TRANSPARENT));
		Values.userInfoUI = userInfoUI;
		sashForm_1.setWeights(new int[] {162, 880});
		//面板宽度固定的方法
		sashForm.addControlListener(new ControlAdapter() {
			@Override
			public void controlResized(ControlEvent e) {
				sashForm.setWeights(new int[] {102, sashForm.getSize().y - 136, 34});
			}
		});
		sashForm_1.addControlListener(new ControlAdapter() {
			@Override
			public void controlResized(ControlEvent e) {
				sashForm_1.setWeights(new int[] {85, sashForm_1.getSize().y - 85});
			}
		});


		Values.stackLayout.topControl = Values.userInfoUI;

		//切换副界面
		button1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {

				Composite topControl = Values.userInfoUI;
				Values.userInfoUI = new UserInfoUI(composite_3, SWT.NONE);
				topControl = Values.userInfoUI;
				Values.stackLayout.topControl = topControl;
				composite_3.layout();
			}			
		});
		//联系人
		button2.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				Composite topControl = Values.contactsUI;
				Values.contactsUI = new ContactsUI(composite_3, SWT.NONE);
				topControl = Values.contactsUI;
				Values.stackLayout.topControl = topControl;
				composite_3.layout();
			}
		});
		//点击购票
		button3.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				Composite topControl = Values.ticketingUI;
				Values.ticketingUI = new TicketingUI(composite_3, SWT.NONE);
				topControl = Values.ticketingUI;
				Values.stackLayout.topControl = topControl;
				composite_3.layout();
			}
		});
		//点击查询订单
		button4.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				Composite topControl = Values.orderUI;
				Values.orderUI = new OrderUI(composite_3, SWT.NONE);
				topControl = Values.orderUI;
				Values.stackLayout.topControl = topControl;
				composite_3.layout();
			}

		});
		//退出登录
		button_3.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				UserLoginUI ul = new UserLoginUI();
				shell.dispose();
				ul.open();				
			}
		});
		//注销账号
		button_4.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				//MessageDialog.openInformation(shell, "警告", "确定要注销吗？注销后无法回复！！！");
				String account = Values.currentLoginUser.get("u_name").toString();
				System.out.println(account);
				UserDao userdao = new UserDao();
				int i = userdao.Cancel(account);
				System.out.println(i);
				//条件判断
				if(i > 0) {  //说明执行SQL语句成功
					SWTUtil.showMessage(shell, "提示", "账号已注销");
					UserLoginUI ul = new UserLoginUI();
					shell.dispose();
					ul.open();		

				}else {
					SWTUtil.showMessage(shell, "提示", "账号注销失败");
				}

			}
		});	

	}

}


